#!/usr/bin/env node

//Routes File

'use strict'

/* MODULE IMPORTS */
const Router = require('koa-router');
//const jimp = require('jimp')

const apiResult = require('../modules/apiResult');
const userSession = require('../lib/userSessionUnit');
const items = require('../modules/itemsModules');

const router = new Router();

/**
 * The secure home page.
 *
 * @name Home Page
 * @route {GET} /
 * @authentication This route requires cookie-based authentication.
 */
router.get('/suggest', async ctx => {
	await ctx.render('suggest')
});


router.get('/suggest/getSuggestItems', async ctx => {
	try {
		let pageIndex = ctx.query.pageIndex || 1;
		let pageSize = ctx.query.pageSize || 10;
		let search = ctx.query.search;
		let itemId = ctx.query.itemId;

		let userId = userSession.getUserId(ctx);

		let result = await items.getSuggestItemsAsync(userId, itemId, pageIndex, pageSize, search);

		result.list = result.list.map(item => {
			return {
				description: item.description,
				id: item.id,
				intime: item.intime,
				name: item.name,
				photo: item.photo,
				thumbnail: item.thumbnail,
				swapItems: item.swapItems
			};
		});

		return ctx.body = new apiResult(true, 'get items success', result);
	} catch (e) {
		return ctx.body = new apiResult(false, e);
	}
});

module.exports = router;
